﻿using AutoMapper;
using Commonality.ReturnView;
using IService.Managemen;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Models.Managemen;
using ModelToDto.Managemen;
using SqlSugar;
using System.ComponentModel.Design;
using System.Linq.Expressions;

namespace ManagementPlatform.Controllers
{
    /// <summary>
    /// 菜单
    /// </summary>
    public class MenueController : BaseController
    {
        IMapper _mapper { get; set; }
        IMenueService _menuCommandService { get; set; }
        public MenueController(IMapper mapper, IMenueService menuCommandService)
        {
            this._mapper = mapper;
            this._menuCommandService = menuCommandService;
        }
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme, Policy = "PlatAuthoriPloicy")]
        [HttpGet]
        public IActionResult Index([FromQuery] string? searchName, [FromQuery] int leveL = 0)
        {

            var exp = Expressionable.Create<Mp_Menue>();
            exp.AndIF(!string.IsNullOrEmpty(searchName), it => it.Meu_Name.Contains(searchName));
            var toExp = exp.ToExpression();
            var result = _menuCommandService.GetMenueList(toExp, leveL);
            var resultMp = _mapper.Map<List<Mp_Menue>, List<MenueDTO>>(result);
            return View(resultMp);
        }
        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuList([FromQuery] string? searchName, [FromQuery] int leveL = 0)
        {
            var exp = Expressionable.Create<Mp_Menue>();
            exp.AndIF(!string.IsNullOrEmpty(searchName), it => it.Meu_Name.Contains(searchName));
            var toExp = exp.ToExpression();
            var result = _menuCommandService.GetMenueList(toExp, leveL);
            var resultMp = _mapper.Map<List<Mp_Menue>, List<MenueDTO>>(result);
            return View(resultMp);
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menueDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenue([FromBody] AddMenueDTO menueDTO)
        {
            if (ModelState.IsValid)
            {
                var ent = _mapper.Map<AddMenueDTO, Mp_Menue>(menueDTO);
                var result = _menuCommandService.Insert<Mp_Menue>(ent);
                if (result > 0)
                {
                    return Ok();
                }
                else
                {
                    return new JsonResult(new ErrorInfo() { Code = 601, Message = "添加失败" });
                }
            }
            return Ok();

        }
        /// <summary>
        /// 获取此菜单下所有按钮
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SelectListItem>> AssigMuneBtn([FromQuery] int menuId)
        {
            return await _menuCommandService.GetAllMunBtn(menuId);
        }
    }
}
